require 'csv'
require 'open-uri'

def get_probabilities(values)
  ret = []
  (3..6).map do |i|
    values[i][3..5].to_f
  end  
end

def get_choices(trials)
  trials.map do |t|
    t.split(%r{\D})[1].to_i
  end
end


def get_rewards(trials)
  trials.map do |t|
    t.split(%r{\D})[2].to_i
  end
end

def get_times(trials)
  trials.map do |t|
    t.split(%r{\D})[3].to_i
  end
end

def get_right_arm_choices(choices, probabilities)
  right_arm = probabilities.rindex(probabilities.max)
  
  choices.map do |t|
    (t == right_arm) ? 1 : 0
  end
end

def number_of_switches(arr)
  number_of_switches = 0
  arr.each_with_index do |obj, i|
    if (i!=0 and arr[i-1] != obj)
      number_of_switches+=1
    end
  end
  number_of_switches
end


def get_switches(choices)
  switches = []
  choices.each_with_index do |obj, i|
    if (i!=0 and choices[i-1] != obj)
      switches << 1
    else
      switches << 0
    end
  end

  switches
end


def lose_shift_rate(choices, rewards)
  
  nonrewarded = 0
  nonrewarded_shifts = 0
  rewards[0, rewards.count-2].each_with_index do |reward, i|
    
    if reward == 0
      nonrewarded+=1
      if choices[i+1] != choices[i]
        nonrewarded_shifts+=1
      end
    end
  end
  
  nonrewarded_shifts.to_f / nonrewarded.to_f
end


def parse_line(line)
  values = CSV.parse_line(line, :col_sep => ';')
  
  probabilities = get_probabilities(values)
  trials = values.slice(7,100)
  choices = get_choices(trials)
  choices_of_right_arm = get_right_arm_choices(choices, probabilities)
  rewards = get_rewards(trials)
  switches = get_switches(choices)
  
  ret = {
    :ip => values[0],
    :name => values[2],
    :probabilities => probabilities,
    :choices => choices,
    :rewards => rewards,
    :score => rewards.inject(0,:+),
    :switches => switches,
    :choices_of_right_arm => choices_of_right_arm,
    :rscore => choices_of_right_arm.inject(0, :+),
    :times => get_times(trials)
  }
  
  ret[:switches_after_25_trials] = switches[25,100].count {|x| x == 1}
  ret[:switches_after_50_trials] = switches[50,100].count {|x| x == 1}
  ret[:switches_after_75_trials] = switches[75,100].count {|x| x == 1} 

  
  ret[:lose_shift_rate_before] = lose_shift_rate(choices[0,24], rewards[0,24])
  ret[:lose_shift_rate_after] = lose_shift_rate(choices[49,99], rewards[49,99])
  
  ret
end

def parse_file(f)
  ret = []
  f.each_line do |l|
      ret << parse_line(l)
  end
  ret
end

def parse_url(url)
  ret = []
  open(url) do |f|
    ret << parse_file(f)
  end
  ret
end
  

def parse_all()
  res = []
  open('http://incc.eryxsoluciones.com.ar/resultados.csv') do |f|
    res+= parse_file(f)
  end
  
  open('http://incc.eryxsoluciones.com.ar/exp/resultados_x.csv') do |f|
    res+= parse_file(f)
  end
  
  #File.open('web/resultadosdefinitivos.csv') do |f|
  #  res+= parse_file(f)
  #end
  
  res
end

@res = parse_all

CSV.open("lose_shift_rate_before_25.csv", "wb") do |f|
  #f << (@res.map {|x| x[:lose_shift_rate_before]}).sort
end

CSV.open("lose_shift_rate_after_50.csv", "wb") do |f|
  #f << (@res.map {|x| x[:lose_shift_rate_after]}).sort
end


@rscores = (@res.map {|x| x[:rscore]}).sort
@scores = (@res.map {|x| x[:score]}).sort
